/*
 * main.c
 *
 *  Created on: 2011-11-3
 *      Author: Octopus
 */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <math.h>
#include <windows.h>

#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/gl.h>

#define EXTENT 1000.0f
#define EXTENT_UNIT 10.0f
void setupRenderStatus(){
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glEnable(GL_DEPTH_TEST);
	glColor3f(0.0f, 1.0f, 0.0f);

}
static GLfloat rotX = 0.0f;
static GLfloat rotY = 0.0f;

void display() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glPushMatrix();
	//view point(0.0f, 00.0f, 500.0f), rotated;
	glTranslatef(0.0f, 0.0f, -500.0f);
	glRotatef(rotX, 0.0f, 1.0f, 0.0f);
	glRotatef(rotY, 1.0f, 0.0f, 0.0f);

	glColor3f(0.0f, 1.0f, 0.0f);
	glBegin(GL_LINES);
		//z axis
		glVertex3f(0.0f, 0.0f, EXTENT);
		glVertex3f(0.0f, 0.0f, -EXTENT);
		//y axis
		glVertex3f(0.0f, EXTENT, 0.0f);
		glVertex3f(0.0f, -EXTENT, 0.0f);
		//x axis
		glVertex3f(EXTENT, 0.0f, 0.0f);
		glVertex3f(-EXTENT, 0.0f, 0.0f);
		glColor3f(0.2f, 0.2f, 0.2f);
		for(GLfloat step = -EXTENT; step <= EXTENT; step += EXTENT_UNIT){
			//lines paralleled to z axis
			glVertex3f(step, 0.0f, EXTENT);
			glVertex3f(step, 0.0f, -EXTENT);
			//lines paralleled to x axis
			glVertex3f(EXTENT, 0.0f, step);
			glVertex3f(-EXTENT, 0.0f, step);
		}
	glEnd();
	glPopMatrix();
	glutSwapBuffers();
}
void reshape(GLsizei w, GLsizei h){
	GLfloat aspectRatio;
	if(h == 0)
		h = 1;
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	aspectRatio = (GLfloat)w / (GLfloat)h;
	gluPerspective(45.0f, aspectRatio, 1.0f, 800.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
static int orginX = 0;
static int orginY = 0;
static GLboolean pressed = GL_FALSE;
void mouseFunc(int button, int state, int x, int y){
	if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
		pressed = GL_TRUE;
		orginX = x;
		orginY = y;
	}else if(button == GLUT_LEFT_BUTTON && state == GLUT_UP){
		pressed = GL_FALSE;
	}
}
void motionFunc(int x, int y){
	if(!pressed)
		return;
	int deltaX = x - orginX;
	int deltaY = y - orginY;
	rotX += deltaX / 10.0f;
	rotY += deltaY / 10.0f;
	orginX = x;
	orginY = y;
	glutPostRedisplay();
}
int main(int argc, char **argv) {

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

	glutCreateWindow("Hello,world!");
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMouseFunc(mouseFunc);
	glutMotionFunc(motionFunc);

	setupRenderStatus();
	glutMainLoop();
	return 0;
}





